package com.ujs.edu.medicine.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.ujs.edu.medicine.dao.*;
import com.ujs.edu.medicine.dataobject.*;
import com.ujs.edu.medicine.util.Result;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @Date: Created in 22:33 2020/8/24
 * Description: 药店类
 * Modified By:
 */
@RestController
public class ShopController {

    @Autowired
    YunjiayiShopMapper yunjiayiShopMapper;
    @Autowired
    YunjiayiUserMapper yunjiayiUserMapper;
    @Autowired
    CollectUSMapper collectUSMapper;
    @Autowired
    GradeUSMapper gradeUSMapper;
    @Autowired
    YunjiayiMedicineMapper yunjiayiMedicineMapper;
    @Autowired
    HttpServletRequest request;

    public String gettoken(){
       return request.getHeader("Authorization");
    }

    /*获取药店列表*/
    @RequestMapping("/api/shop/shopList")
    public Result shopList(){
        Result result=new Result();

        List<YunjiayiShop> shops=yunjiayiShopMapper.selectAll();
        if (shops!=null){
            result.setCode(0);
            result.setMessage("查询成功");
            result.setData(shops);
        }else {
            result.setCode(1);
            result.setMessage("查询失败");
        }

        return result;
    }

    /*获取药店具体信息*/
    @RequestMapping("/api/shop/getShopInfo")
    public Result getShopInfo(@RequestParam("ID") Integer id){
        Result result=new Result();
        JSONObject object=new JSONObject();
        String token=gettoken();
        int myuserid=0;
        try {
            myuserid=JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登陆");
            return result;
        }

        YunjiayiShop shop=yunjiayiShopMapper.selectByPrimaryKey(id);
        if (shop!=null){
            CollectUS collectUS=collectUSMapper.selectByUId(myuserid,id);
            if (collectUS!=null){
                shop.setIscolletc1(true);
            }else {
                shop.setIscolletc1(false);
            }

            try {
                object.put("picture",shop.getShopimage());
                object.put("See",shop.getDescription());
                object.put("number",shop.getTelephone());
                object.put("name",shop.getShopname());
                if (shop.getShopgrade()==null){
                    object.put("grade",0);
                }else {
                    object.put("grade",shop.getShopgrade());
                }
                object.put("isCollect",shop.getIscolletc1());
                object.put("ini_delivery_fee",shop.getInideliveryfee());
                object.put("delivery_fee",shop.getDeliveryfee());
                object.put("address",shop.getAddress());
            }catch (Exception e){
                result.setCode(1);
                result.setMessage("请检查数据库信息是否完整");
                return result;
            }

            result.setData(object);
            result.setMessage("查询成功");
            result.setCode(0);
        }else {
            result.setCode(1);
            result.setMessage("查询失败");
        }
        return result;
    }

    /*收藏/取消收藏药店*/
    @RequestMapping("/api/shop/collectShop")
    public Result collectShop(@RequestBody JSONObject object){
        Result result=new Result();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid= JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登陆");
            return result;
        }

        CollectUS collectUS=collectUSMapper.selectByUId(myuserid,object.getInteger("shopID"));
        if (collectUS!=null){
            int dellike=collectUSMapper.deleteByPrimaryKey(collectUS.getId());
            if (dellike!=0){
                result.setCode(0);
                result.setMessage("取消收藏成功");
            }else {
                result.setCode(1);
                result.setMessage("取消收藏失败");
            }
            return result;
        }

        collectUS=new CollectUS();
        collectUS.setShopid(object.getInteger("shopID"));
        collectUS.setUid(myuserid);
        int col=collectUSMapper.insert(collectUS);

        if (col==1){
            result.setCode(0);
            result.setMessage("收藏成功");
        }else {
            result.setCode(1);
            result.setMessage("收藏失败");
        }

        return  result;
    }


    /*获取收藏列表*/
    @RequestMapping("/api/shop/getCollect")
    public Result getCollect(){
        Result result=new Result();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid=JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还未登陆，请登陆");
            return result;
        }

        List<CollectUS> collectUSES=collectUSMapper.getShopsByuId(myuserid);
        if (collectUSES==null){
            result.setCode(1);
            result.setMessage("没有收藏的商店");
            return result;
        }

        ArrayList<YunjiayiShop>shops=new ArrayList<>();
        for (CollectUS collectUS:collectUSES){
            shops.add(collectUS.getShop());
        }

        if (shops!=null){
            result.setCode(0);
            result.setMessage("查询成功");
            result.setData(shops);
        }else {
            result.setCode(1);
            result.setMessage("查询失败");
        }

        return result;
    }

    /*添加药店评分*/
    @RequestMapping("/api/shop/shopGrade")
    public Result shopGrade(@RequestBody GradeUS gradeUS){
        Result result=new Result();


        int gra=gradeUSMapper.insert(gradeUS);
        if (gra==1){
            result.setCode(0);
            result.setMessage("添加成功");
        }else {
            result.setMessage("添加失败");
            result.setCode(1);
        }

        return result;
    }


    /*上传药店具体信息*/
    @RequestMapping("/api/shop/UpShopInfo")
    public Result upShopInfo(YunjiayiShop yunjiayiShop){
        Result result=new Result();

        YunjiayiShop yunjiayiShop1=yunjiayiShopMapper.selectByShopName(yunjiayiShop.getShopname());
        if (yunjiayiShop1==null){
            result.setCode(1);
            result.setMessage("没有这个药店");
            return result;
        }

        yunjiayiShop.setShopid(yunjiayiShop1.getShopid());
        int shopInfo=yunjiayiShopMapper.updateByPrimaryKey(yunjiayiShop);
        if (shopInfo==1){
            result.setCode(0);
            result.setMessage("上传成功");
            result.setData(yunjiayiShopMapper.selectByShopName(yunjiayiShop.getShopname()));
        }else {
            result.setCode(1);
            result.setMessage("上传失败");
        }

        return result;
    }

    /*搜索药店/药品*/
    @RequestMapping("/api/shop/search")
    public Result searchInfo(@RequestBody Map<String,String> map){
        Result result=new Result();

        if (Integer.parseInt(map.get("kind"))==1){
            List<YunjiayiShop>shops=yunjiayiShopMapper.selectShopLike(map.get("content"));
            if (shops!=null){
                result.setCode(0);
                result.setMessage("查询成功");
                result.setData(shops);
            }else {
                result.setCode(1);
                result.setMessage("查询失败");
            }
        }else if (Integer.parseInt(map.get("kind"))==0){
            List<YunjiayiMedicine>medicines=yunjiayiMedicineMapper.selectLike(map.get("content"));
            if (medicines!=null){
                result.setMessage("查询成功");
                result.setCode(0);
                result.setData(medicines);
            }else {
                result.setCode(1);
                result.setMessage("查询失败");
            }
        }

        return result;
    }


    /*修改药店具体信息*/
    @RequestMapping("/api/shopManage/editShopInfo")
    public Result editShopInfo(@RequestBody JSONObject object){
        Result result=new Result();

        YunjiayiShop shop=null;
        YunjiayiUser user=null;

        try {

            shop=yunjiayiShopMapper.selectByPrimaryKey(object.getInteger("id"));
            shop.setShopid(object.getInteger("id"));
            shop.setShopname(object.getString("name"));
            shop.setTelephone(object.get("phone").toString());
            shop.setShopimage(object.getString("picture"));
            shop.setDescription(object.getString("see"));
            shop.setInideliveryfee(object.getFloat("ini_delivery_fee"));
            shop.setDeliveryfee(object.getFloat("delivery_fee"));
            shop.setAddress(object.getString("address"));

            user=yunjiayiUserMapper.selectByPrimaryKey(shop.getShopid());

            user.setUser_Id(shop.getShopid());
            user.setPhonenumber(shop.getTelephone());
            user.setUnit(shop.getAddress());
            user.setRealname(shop.getShopname());
            user.setAvatar(shop.getShopimage());
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("请输入完整信息,或信息有误");
            return result;
        }

        try {
            int updateShop=yunjiayiShopMapper.updateByPrimaryKey(shop);
            int updateUser=yunjiayiUserMapper.updateByPrimaryKey(user);

            result.setCode(0);
            result.setMessage("更新成功");
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("更新失败");
        }

        return result;
    }

    /*添加药店*/
    @RequestMapping("/api/shopManage/addShop")
    public Result addShop(@RequestBody YunjiayiShop shop){
        Result result=new Result();

        YunjiayiShop shop1=yunjiayiShopMapper.selectByShopName(shop.getShopname());
        if (shop1!=null){
            result.setCode(1);
            result.setMessage("已经存在这个药店");
            return  result;
        }

        int addsh=yunjiayiShopMapper.insert(shop);
        if (addsh!=0){
            result.setCode(0);
            result.setMessage("添加成功");
            result.setData(shop.getShopid());
        }else {
            result.setCode(1);
            result.setMessage("添加失败");
        }

        return result;
    }


    /*下架药店*/
    @RequestMapping("/api/shopManage/delete")
    public Result deleteShop(@RequestBody Map<String,Integer> map){
        Result result=new Result();


        int delsh=yunjiayiShopMapper.deleteByPrimaryKey(map.get("ID"));
        int delUs=yunjiayiUserMapper.deleteByPrimaryKey(map.get("ID"));
        if (delsh!=0){
            result.setCode(0);
            result.setMessage("删除成功");
        }else {
            result.setCode(1);
            result.setMessage("删除失败");
        }

        return result;
    }
}
